package lib.bone.access.module.dict;

import lib.bone.access.constant.ResearchTypeConst;
import lib.bone.access.module.dict.entity.*;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author kindear
 * @date 2022-4-4
 */
@Slf4j
@Service
public class DictServiceCtl {
    @Autowired(required = false)
    private List<DictService> dictServiceList;

    /**
     * 新建记录
     * @param o
     * @return
     */
    public ResponseDTO saveOne(Object o){
        for (DictService service:dictServiceList){
            log.info(service.getServiceType());
            if (service.getServiceType().equals(ResearchTypeConst.AWARD) && o instanceof NepuAwardDict){
                //log.info(service.getServiceType());
                return ResponseDTO.succData(service.saveOne(o));
            }
            else if (service.getServiceType().equals(ResearchTypeConst.PAPER) && o instanceof NepuPaperDict){
                return ResponseDTO.succData(service.saveOne(o));
            }
            else if (service.getServiceType().equals(ResearchTypeConst.BOOK) && o instanceof NepuBookDict){
                return ResponseDTO.succData(service.saveOne(o));
            }
            else if (service.getServiceType().equals(ResearchTypeConst.THINKTANK) && o instanceof NepuThinkTankDict){
                return ResponseDTO.succData(service.saveOne(o));
            }
            else if (service.getServiceType().equals(ResearchTypeConst.TOPIC) && o instanceof NepuTopicDict){
                return ResponseDTO.succData(service.saveOne(o));
            }
            else if (service.getServiceType().equals(ResearchTypeConst.PROPERTY) && o instanceof NepuPropertyDict){
                return ResponseDTO.succData(service.saveOne(o));
            }
            else if (service.getServiceType().equals(ResearchTypeConst.SUBJECT) && o instanceof NepuSubjectDict){
                return ResponseDTO.succData(service.saveOne(o));
            }
        }
        return ResponseDTO.wrap(ResponseCodeConst.SERVICE_UNAVAILABLE);
    }

    /**
     * 更新记录
     * @param o
     * @return
     */
    public ResponseDTO updateOne(Object o,String serviceType){
        for (DictService service:dictServiceList){
            if (service.getServiceType().equals(serviceType)) {
                return ResponseDTO.succData(service.updateOne(o));
            }
        }
        return ResponseDTO.wrap(ResponseCodeConst.SERVICE_UNAVAILABLE);
    }

    /**
     * 根据ID 检索
     * @param o
     * @return
     */
    public Optional<Object> getById(Object o,String serviceType){
        for (DictService service:dictServiceList){
            if (service.getServiceType().equals(serviceType)) {
                return service.getById(o);
            }
        }
        throw new BusinessException(ResponseCodeConst.SERVICE_UNAVAILABLE);
    }

    /**
     * 根据ID 删除
     * @param o
     * @return
     */
    public ResponseDTO deleteById(Object o,String serviceType){
        for (DictService service:dictServiceList){
            if (service.getServiceType().equals(serviceType)) {
                return ResponseDTO.succData(service.deleteById(o));
            }
        }
        return ResponseDTO.wrap(ResponseCodeConst.SERVICE_UNAVAILABLE);
    }
    public PageResultDTO getAll(Pageable pageable, Example example,String serviceType){
        for (DictService service:dictServiceList){
            if (service.getServiceType().equals(serviceType)){
                return service.getAll(pageable,example);
            }
        }
        throw new BusinessException(ResponseCodeConst.SERVICE_UNAVAILABLE);
    }
}
